package imgsei.wcj.com.medalmodule.model;

import android.content.Context;

import java.util.Map;

import imgsei.wcj.com.commonbusiness.base.model.BaseModelImp;
import imgsei.wcj.com.commonbusiness.interfaces.callback.IBaseRequestCallBack;
import imgsei.wcj.com.commonbusiness.interfaces.model.BaseModel;
import imgsei.wcj.com.commonbusiness.utils.LogUtils;
import imgsei.wcj.com.medalmodule.bean.WeatherInfoBean;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WeatherModelImp extends BaseModelImp implements BaseModel<WeatherInfoBean> {
    private static final String TAG = "WeatherModelImp";

    public WeatherModelImp(Context context) {
        super(context);
    }

    @Override
    public void loadWeather(String url,Map<String,String> params,
                            final IBaseRequestCallBack<WeatherInfoBean> iBaseRequestCallBack) {
        if (compositeSubscription != null) {
            compositeSubscription.add(service.servicePost(url,getGson().toJson(params))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<String>() {
                        @Override
                        public void onStart() {
                            super.onStart();
                            iBaseRequestCallBack.beforeRequest();
                        }

                        @Override
                        public void onCompleted() {
                            iBaseRequestCallBack.requestComplete();
                        }

                        @Override
                        public void onError(Throwable e) {
                            iBaseRequestCallBack.requestError(e);
                        }

                        @Override
                        public void onNext(String data) {
                            LogUtils.showLogD(TAG,"RETROFIT-ON-NEXT:"+data);

                            //iBaseRequestCallBack.requestSuccess(getGson().fromJson(data,WeatherInfoBean.class));
                        }
                    }));
        }
    }

    @Override
    public void onUnsubscribe() {
        if (compositeSubscription.isUnsubscribed()) {
            compositeSubscription.clear();
            compositeSubscription.unsubscribe();
        }
    }
}
